package models

import "time"
import "gorm.io/gorm"
import "go_part/utils"
//import "fmt"

type LevelDevice struct{
	gorm.Model
	Name string
	DeviceID string
	LevelLocalID string
	CompanyID string
	Phone string 
	Email string 
	DeviceTypeID string
	Identity string
	Longitude string
	Latitude string
	ClientIP string
	ClientPort string
	Salt string
	LoginTime time.Time
	IsLogout bool
	DeviceInfo string
 }

func (table *LevelDevice) TableName() string {

	return "level_device_basic"
}

func GetLevelDeviceList() []*LevelDevice { 
	data := make([]*LevelDevice,10)
	utils.DB.Find(&data)
	// for _,v : = range data {
	// 	fmt.Println(v)
	// }
	return data
}

func FindLevelDeviceByName(name string,CompanyID string) LevelDevice {
	user := LevelDevice{}
	utils.DB.Where("name = ? and company_id = ?",name,CompanyID).Find(&user)
	return user
}

func FindLevelDeviceByID(id uint) LevelDevice {
	user := LevelDevice{}
	utils.DB.Where("id = ?",id).First(&user)
	return user
}

func FindLevelDeviceByTypeIDAndCompanyID(id string,CompanyID string) []*LevelDevice {
	user := make([]*LevelDevice,100)
	utils.DB.Where("level_local_id = ? and company_id = ?",id,CompanyID).Find(&user)
	return user
}

func CreateLevelDevice(user LevelDevice) *gorm.DB {
	
	return utils.DB.Create(&user)
}

func DeleteLevelDevice(user LevelDevice) *gorm.DB {
	
	return utils.DB.Delete(&user)
}

func UpdateLevelDevice(user LevelDevice) *gorm.DB {
	
	return utils.DB.Model(&user).Updates(LevelDevice{Name: user.Name,DeviceTypeID: user.DeviceTypeID,Longitude: user.Longitude, Latitude: user.Latitude, DeviceInfo: user.DeviceInfo})
}